<template>
  <div class="postlist">
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 头部 -->
      <el-breadcrumb separator="/" style="margin-bottom: 20px">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
          ><a href="javascript:;">文章管理</a></el-breadcrumb-item
        >
        <el-breadcrumb-item>文章列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 表格 -->
      <el-table :data="postlist" border style="width: 100%">
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="title" label="标题" width="650">
        </el-table-column>
        <el-table-column prop="type" label="类型" width="100">
          <template slot-scope="scope">{{
            scope.row.type == 1 ? "文章" : "视频"
          }}</template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者" width="100">
        </el-table-column>
        <el-table-column label="操作" width="220">
          <!-- 使用自定义模板 -->
          <!-- scope 就是当前的数据行对象，包含当行的对象，索引。。。
        scope.row 就是当前的行数据 我们以后进行操作的时候，组要是对这个值进行处理
         -->
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="handleEdit(scope)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="分享"
              placement="top"
            >
              <el-button type="success" icon="el-icon-share"></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
            >
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { postList } from "@/apis/post.js";
export default {
  data() {
    return {
      postlist: [], // 遍历的数据
      pageIndex: 1, //  当前的页码
      pageSize: 3, // 当前页的数量
      total: 0, // 数据总数数
    };
  },
  methods: {
    // 点击编辑时
    handleEdit(scope) {
      console.log(scope.row.id);
      // this.$router.push({ name: "login" });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      // console.log(val);
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.init();
    },
    async init() {
      let res = await postList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      });
      console.log(res);
      this.postlist = res.data.data;
      this.total = res.data.total;
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<style>
</style>